import Vue from 'vue'
import Router from 'vue-router'
// const Foo = () => import(/* webpackChunkName: "group-foo" */ './Foo.vue')

// login_home_welcome 合并打包到同一个js文件中
//import Login from './components/Login.vue'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ './components/Login.vue')
//import Home from './components/Home.vue'
const Home = () => import(/* webpackChunkName: "group-foo" */ './components/Home.vue')
//import Welcome from './components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "group-foo" */ './components/Welcome.vue')


//import Users from './components/user/Users.vue'
const Users = () => import(/* webpackChunkName: "users_rights_roles" */ './components/user/Users.vue')
///import Rights from './components/power/Rights.vue'
const Rights = () => import(/* webpackChunkName: "users_rights_roles" */ './components/power/Rights.vue')
//import Roles from './components/power/Roles.vue'
const Roles = () => import(/* webpackChunkName: "users_rights_roles" */ './components/power/Roles.vue')


//import Categories from './components/goods/Categories.vue'
const Categories = () => import(/* webpackChunkName: "categories_params" */ './components/goods/Categories.vue')
//import Params from './components/goods/Params.vue'
const Params = () => import(/* webpackChunkName: "categories_params" */ './components/goods/Params.vue')


//import Goods from './components/goods/Goods.vue'
const Goods = () => import(/* webpackChunkName: "goods_add" */ './components/goods/Goods.vue')
//import Add from './components/goods/Add.vue'
const Add = () => import(/* webpackChunkName: "goods_add" */ './components/goods/Add.vue')


//import Order from './components/order/Order.vue'
const Order = () => import(/* webpackChunkName: "order_reports" */ './components/order/Order.vue')
//import Reports from './components/reports/Reports.vue'
const Reports = () => import(/* webpackChunkName: "order_reports" */ './components/reports/Reports.vue')

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home', component: Home, redirect: '/Welcome', children: [
        { path: '/Welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Categories },
        { path: '/params', component: Params },
        { path: '/goods', component: Goods },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Reports },
      ]
    }
  ]
})
// 为路由对象，添加 beforeEach 导航守卫
router.beforeEach((to, from, next) => {
  // to 代表将要访问的页面路径
  // from 代表从那个页面路径跳转来的
  // next 代表放行的函数
  // 如果 用户访问的是 登录页   直接放行
  if (to.path === '/login') return next()
  // 从 sessionStorage 中获取到 保存的 token 值
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有 token 强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})

export default router